package com.fept.msgcore.msgserver.protocol.model.body;

/**
 * Created by Srma on 2/9/2017.
 */
public class SimpleJsonValueBody extends BaseBody {

    /**
     *
     */
    private static final long serialVersionUID = 6890384289152003918L;

    private String jsonValue;

    private byte[] jsonBytes;

    /**
     *
     */
    @Override
    public byte[] pack() {
        byte[] jsonValueBytes = null;
        if (jsonValue!=null && jsonValue.length()>0) {
            jsonValueBytes = this.jsonValue.getBytes();
        }
        else{
            jsonValueBytes = this.jsonBytes;
        }

        byte[] data = new byte[jsonValueBytes.length];

        System.arraycopy(jsonValueBytes, 0, data, 0, jsonValueBytes.length);

        this.setValue(data);
        this.setLength(data.length);

        return super.pack();
    }

    @Override
    public void unpack(byte[] data, int offset) {
        super.unpack(data, offset);

        data = this.getValue();
        offset = 0;

        byte[] jsonValueBytes = new byte[this.getLength()];
        System.arraycopy(data, offset, jsonValueBytes, 0, jsonValueBytes.length);
        offset += jsonValueBytes.length;

        this.jsonValue = new String(jsonValueBytes);
    }

    /**
     * @return the jsonValue
     */
    public String getJsonValue() {
        return jsonValue;
    }

    /**
     * @param jsonValue
     *            the jsonValue to set
     */
    public void setJsonValue(String jsonValue) {
        this.jsonValue = jsonValue;
    }

    public byte[] getJsonBytes() {
        return jsonBytes;
    }

    public void setJsonBytes(byte[] jsonBytes) {
        this.jsonBytes = jsonBytes;
    }

}
